<?php
 
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2015/6/25
 * Time: 14:03
 */
class RoleAction extends CommAction
{
    public function index()
    {
        $roleModel = D('Role');
        $map = array();
        $map['id'] = array('neq',2);
        $roles=$roleModel->where($map)->select();
        
        $this->assign('roles', $roles);
        $this->display();
    }

    public function log(){
        import('ORG.Util.Page');
        $count = M('log')->where($map)->count();// 查询满足要求的总记录数
        $Page = new Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $map = array();
        $results = M('log')->where($map)->order('createtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($results as $key => $value) {
            $results[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
        }
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rs',$results);
        $this->display();
    }

    /**
     * 查看角色权限详细
     */
    public function detail(){
        // $this->checkRole();
        // $actionModel = D('Action');
        // $map['id'] = array('neq',2);
        // $actions = $actionModel->where($map)->select();
        // $this->assign('addactions', $actions);

        $roleId = trim(I('get.id'));
        $roleModel = D('Role');
        $actionModel = D('Action');
        $map = array();
        $map['id'] = $roleId;
        $name = M('role')->where($map)->getField('name');
        $this->assign('name',$name);
        $actions = $roleModel->where($map)->getField('action');
        $act = $roleModel->where($map)->getField('act');
        $actionArr = explode(',', $actions);
        $actD = explode(',', $act);
        $actionDetail = array();
        foreach ($actionArr as $key => $value) {
            $map = array();
            $map['id'] = $value;
            $actionDetail[$key]['id'] = $actionModel->where($map)->getField('id');
            $actionDetail[$key]['module'] = $actionModel->where($map)->getField('module');
            $actionDetail[$key]['info'] = $actionModel->where($map)->getField('info');
            $actionDetail[$key]['act'] = $actD[$key];
        }
        $this->assign('details', $actionDetail);
        $actionModel = D('Action');
        $ss['id'] = array('neq',2);
        $actions = $actionModel->where($ss)->select();
        //找出用户已有的权限

        foreach ($actionDetail as $key => $value) {
            $id = $actionDetail[$key]['id'];
            foreach ($actions as $nkey => $nvalue) {
                if ($actions[$nkey]['id'] == $id) {
                    $actions[$nkey]['has'] = 1;
                    $actions[$nkey]['act'] = $actionDetail[$key]['act'];
                }
            }
        }
        $this->assign('addactions', $actions);
        if (IS_POST) {
            $this->checkRole();
            if (empty($_POST)) {
                $this->error('请选择权限');
            }
            $data = $_POST;
            $role = array();
            $act = array();
            foreach ($data as $key => $value) {
                if (is_numeric($key)) {
                    $role[$key] = $key;
                    $act[$key . '_radio'] = $data[$key . '_radio'];
                }
            }
            //判定输入的合法性
            // foreach ($role as $key => $value) {
            //     if (empty($act[$key . '_radio'])) {
            //         //找出$key对应的模块名，进行报错
            //         $map = array();
            //         $map['id'] = $key;
            //         $info = $actionModel->where($map)->getField('info');
            //         $this->error('模块： ' . $info . '  没有分配读写权限，请选择！');
            //     }
            // }
            $str = implode(',', $role);
            $act = implode(',', $act);
            $map = array();
            $map['id'] = $roleId;
            $data = array();
            $data['action'] = $str;
            $data['act'] = $act;
            $data['name'] = $_POST['name'];
            $roleModel->where($map)->data($data)->save();
            $this->addLog($name,$type='角色管理',$action='修改');
            $this->success('修改成功', U('index'));
            exit;
        }
        $this->display();
    }

    public function del(){
        $roleId = trim(I('get.id'));
        $roleModel = D('Role');
        $map = array();
        $map['id'] = $roleId;
        $name = $roleModel->where($map)->getField('name');

        $uid = getLoginUid();
        $adminModel = D('Admin');
        $role = $adminModel->where(array('id'=>$uid))->getField('role'); 
        if($role!=1){
            $this->simpleAlert('只有超级管理员可以进行此项操作');exit;
        }else{
            
            // dump(M()->getLastSql());exit;
            $roles = array();
            if(in_array($roleId, $roles)){
                $this->simpleAlert('此权限不能被删除');exit;
            }else{
                $rs = $roleModel->where(array('id'=>$roleId))->delete();
                if($rs){
                    $this->addLog($name,$type='角色管理',$action='删除');
                    $this->success('删除成功！');exit;
                }else{
                    $this->error('删除失败！');exit;
                }
            }
            
        }
    }

    /**
     * 新增角色
     */
    public function add()
    {
        $this->checkRole();
        $actionModel = D('Action');
        $map['id'] = array('neq',2);
        $actions = $actionModel->where($map)->select();
        $this->assign('addactions', $actions);
        if (IS_POST) {
            $data = $_POST;
            $role = array();
            $act = array();
            foreach ($data as $key => $value) {
                if (is_numeric($key)) {
                    $role[$key] = $key;
                    $act[$key] = $data[$key . '_radio'];
                }
            }
            $actions = $_POST;
            unset($actions['name']);
            $actions = implode(',', $role);
            $act = implode(',', $act);

            $name = trim(I('post.name'));
            $has = M('Role')->where(array('name'=>$name))->find();
            if($has){
                $this->error($name.'已存在');exit;
            }
            $roleModel = D('Role');
            $data = array();
            $data['name'] = $name;
            $data['action'] = $actions;
            $data['act'] = $act;
            $rs = M('Role')->data($data)->add();
            $this->addLog($name,$type='角色管理',$action='添加');
            $this->success('新增成功！', U('index'));
            exit;
        }
        $this->display();
    }

    public function user(){
        redirect(U('User/index'));
    }


    
}